package com.rolerealm.service.impl;

import com.rolerealm.dao.CharacterTagMapper;
import com.rolerealm.domain.entity.CharacterTagEntity;
import com.rolerealm.domain.vo.CharacterTagVO;
import com.rolerealm.service.CharacterTagService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色标签服务实现类
 * 
 * @author RoleRealm Team
 * @version 1.0.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CharacterTagServiceImpl implements CharacterTagService {

    private final CharacterTagMapper characterTagMapper;

    @Override
    public List<CharacterTagVO> getTagsByCharacterId(Long characterId) {
        log.debug("根据角色ID获取标签列表，角色ID：{}", characterId);
        
        List<CharacterTagEntity> tags = characterTagMapper.selectByCharacterId(characterId);
        
        return tags.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<CharacterTagVO> getTagsByCharacterIds(List<Long> characterIds) {
        log.debug("根据角色ID列表批量获取标签，角色ID列表：{}", characterIds);
        
        if (CollectionUtils.isEmpty(characterIds)) {
            return List.of();
        }
        
        List<CharacterTagEntity> tags = characterTagMapper.selectByCharacterIds(characterIds);
        
        return tags.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getCharacterIdsByTagName(String tagName) {
        log.debug("根据标签名称获取角色ID列表，标签名称：{}", tagName);
        
        return characterTagMapper.selectCharacterIdsByTagName(tagName);
    }

    @Override
    public List<CharacterTagVO> getTagsByTagType(String tagType) {
        log.debug("根据标签类型获取标签列表，标签类型：{}", tagType);
        
        List<CharacterTagEntity> tags = characterTagMapper.selectByTagType(tagType);
        
        return tags.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public List<CharacterTagVO> getPopularTags(Integer limit) {
        log.debug("获取热门标签，限制数量：{}", limit);
        
        List<CharacterTagEntity> tags = characterTagMapper.selectPopularTags(limit);
        
        return tags.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public Boolean addTagToCharacter(Long characterId, String tagName, String tagType, String tagColor, String description) {
        log.debug("为角色添加标签，角色ID：{}，标签名称：{}", characterId, tagName);
        
        CharacterTagEntity tag = CharacterTagEntity.builder()
                .characterId(characterId)
                .tagName(tagName)
                .tagType(tagType)
                .tagColor(tagColor)
                .description(description)
                .createdAt(LocalDateTime.now())
                .updatedAt(LocalDateTime.now())
                .build();
        
        int result = characterTagMapper.insert(tag);
        
        log.debug("角色标签添加{}，角色ID：{}，标签名称：{}", result > 0 ? "成功" : "失败", characterId, tagName);
        
        return result > 0;
    }

    @Override
    public Boolean removeTagFromCharacter(Long characterId, Long tagId) {
        log.debug("从角色移除标签，角色ID：{}，标签ID：{}", characterId, tagId);
        
        int result = characterTagMapper.deleteById(tagId);
        
        log.debug("角色标签移除{}，角色ID：{}，标签ID：{}", result > 0 ? "成功" : "失败", characterId, tagId);
        
        return result > 0;
    }

    @Override
    public Boolean updateCharacterTag(Long tagId, String tagName, String tagType, String tagColor, String description) {
        log.debug("更新角色标签，标签ID：{}，标签名称：{}", tagId, tagName);
        
        CharacterTagEntity tag = characterTagMapper.selectById(tagId);
        if (tag == null) {
            return false;
        }
        
        tag.setTagName(tagName);
        tag.setTagType(tagType);
        tag.setTagColor(tagColor);
        tag.setDescription(description);
        tag.setUpdatedAt(LocalDateTime.now());
        
        int result = characterTagMapper.updateById(tag);
        
        log.debug("角色标签更新{}，标签ID：{}，标签名称：{}", result > 0 ? "成功" : "失败", tagId, tagName);
        
        return result > 0;
    }

    /**
     * 转换为标签VO
     */
    private CharacterTagVO convertToVO(CharacterTagEntity entity) {
        CharacterTagVO vo = new CharacterTagVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
